<template>
  <div class="table-box">
    <ProTable ref="proTable" title="商品分品牌列表" row-key="id" :indent="20" :columns="columns" :request-api="getWaresBrandList">
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', initData)"> 新建品牌 </el-button>
      </template>
      <template #isShow="scope">
        <el-switch v-model="scope.row.isShow" size="small" disabled />
      </template>
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="EditPen" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteCategory(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <WaresBrandDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="waresBrand">
import { ref, reactive } from "vue";
import { Inventory } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { getWaresBrandList, createWaresBrand, editWaresBrand, deleteWaresBrand } from "@/api/modules/inventory";
import WaresBrandDialog from "@/views/inventory/components/WaresBrandDialog.vue";
import { CirclePlus, EditPen, Delete } from "@element-plus/icons-vue";

const proTable = ref<ProTableInstance>();
// 新建默认数据
const initData = reactive({
  id: 0,
  isShow: true
});

const columns: ColumnProps<Inventory.ResWaresBrandList>[] = [
  { type: "index", label: "序号", width: 80 },
  { sortable: true, prop: "name", label: "品牌名称", minWidth: 160, align: "left" },
  {
    sortable: true,
    prop: "isShow",
    label: "是否显示",
    minWidth: 100
  },
  { sortable: true, prop: "createTime", label: "创建时间", minWidth: 180 },
  { prop: "operation", label: "操作", width: 220, fixed: "right" }
];

const deleteCategory = async (params: Inventory.ResWaresBrandList) => {
  await useHandleData(deleteWaresBrand, { id: params.id }, `删除【${params.name}】分类`);
  proTable.value?.getTableList();
};

const dialogRef = ref<InstanceType<typeof WaresBrandDialog> | null>(null);
const openDialog = (title: string, row: Partial<Inventory.ResWaresBrandList> = {}) => {
  const params = {
    title,
    row: { ...row },
    isView: title === "查看",
    api: title === "新增" ? createWaresBrand : title === "编辑" ? editWaresBrand : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
